# coding:utf-8

import json
import socket
import datetime
import getinfo
import threading

HOST = '0.0.0.0'
PORT = 9999

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)  #创建一个socket
s.bind((HOST,PORT)) #绑定socket
s.listen(5) #开始监听

def get_test(data):
    params=json.loads(data)
    g_model=getinfo.GetinfoController()
    g_info=g_model.test_get(params)
    return g_info

def tcplink(sock, addr):
    res={'code':0,'msg':'','data':''} #
    print('Accept new connection from %s:%s...' % addr)
    data = sock.recv(1024)
    r=get_test(data)
    res['data']=r
    res_json=json.dumps(res)
    print(type(res_json))
    sock.send(res_json.encode('utf8'))  #向客户端返回数据
    sock.close()
    print('ok')
while True:
    # 接受一个新连接:
    sock, addr = s.accept()
    # 创建新线程来处理TCP连接:
    t = threading.Thread(target=tcplink, args=(sock, addr))
    t.start()
